<!--
        Copyright (c) 2023, LXMS (lxinet.cn).

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<template>
  <lx-modal
      ref="lxModal"
      v-model:isShow="isShow"
      title="角色管理- 分配权限"
      :roleId="roleId"
      :confirm-loading="loading"
      width="600px"
      @ok="updateAuth"
      @open="handleOpen"
  >
    <template #modalBody>
      <a-tree
          v-model:checkedKeys="checkedMenuIds"
          v-if="menuList.length"
          :defaultExpandAll="true"
          checkable
          :fieldNames="fieldNames"
          :tree-data="menuList"
          @check="check"
      >
        <template #title="{ name, id }">
          <span v-if="id === '0-0-1-0'" style="color: #1890ff">{{ name }}</span>
          <template v-else>{{ name }}</template>
        </template>
      </a-tree>
    </template>

  </lx-modal>

</template>


<script>
import lxModal from "@components/baseComponents/lxModal/index.vue";
import lxForm from "@components/baseComponents/lxForm/index.vue"

export default {
  name: "globalNav",
  components: {
    lxModal,
    lxForm
  },
  data() {
    return {
      isShow: false,
      loading: false,
      roleId: 0,
      fieldNames: {
        children: 'children',
        title: 'name',
        key: 'id'
      },
      menuList: [],
      checkedMenuIds: []
    };
  },
  methods: {
    getSimpleMenuList() {
      this.$request({
        url: "/system/menu/simpleList",
        method: "get",
      }).then((res) => {
        console.log(res)
        this.menuList = res.data;
        console.log("this.menuList", this.menuList)
      }).finally(() => {
        this.loading = false;
      })
    },
    getCheckedMenuIds() {
      this.$request({
        url: "/system/roleMenu/listMenuIds?roleId=" + this.roleId,
        method: "get",
      }).then((res) => {
        console.log(res)
        this.checkedMenuIds = res.data;
        this.getSimpleMenuList();
      }).finally(() => {
        this.loading = false;
      })
    },
    updateAuth() {
      this.loading = true;
      this.$request({
        url: "/system/roleMenu/update",
        method: "post",
        data: {
          roleId: this.roleId,
          menuIds: this.checkedMenuIds
        }
      }).then((res) => {
        this.$message.success("更新成功");
      }).finally(() => {
        this.loading = false;
      })
    },
    handleOpen() {
      this.getCheckedMenuIds();

    },
    check() {
      console.log("check=======", this.checkedKeys)
    }

  },
  mounted() {


  }


};
</script>

<style lang="less" scoped>


</style>